package com.spring.annotation_config_demo;

import com.spring.annotation_config_demo.pkg1.MyService01;
import com.spring.annotation_config_demo.pkg2.MyService02;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 使用注解方式注册
 */
public class App {
	public static void main(String[] args) {
		//AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		//MyService bean = context.getBean(MyService.class);
		//bean.hello();

		//第二种注册方法（提供热注册）
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(AppConfig.class);
		//context.refresh();
		//MyService bean = context.getBean(MyService.class);
		//bean.hello();

		//MyService01 myService01 = (MyService01) context.getBean("myService01");

		//若只使用AppConfig中的basePackage范围的扫描，无法扫描到pkg2的bean
		//报错：org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myService02' available
		//针对报错，可以使用ctx.scan()，增加新的扫描范围,然后refresh一次.但是refresh不能多次执行
		context.scan("com.spring.annotation_config_demo.pkg2");
		context.refresh();
		MyService02 myService02 = (MyService02) context.getBean("myService02");
//		System.out.println(myService01.getClass());
		System.out.println(myService02.getClass());

	}
}
